package com.gwt.client.login.client;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.JPasswordField;
import javax.swing.JPanel;
import java.awt.Button;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;


public class LoginUI {

	private JFrame frame;
	private JTextField textField;
	private JLabel lblPassword;
	private JPasswordField passwordField;
	private JButton btnLogin;
	private JLabel lblLogin;

	private static int type;
	/*
	 * 1 : Patient
	 * 2 : Nurse
	 * 3 : Doctor
	 * 4 : Admin
	 */
	private static String textFieldMatch;
	private static char[] passwordMatch;
	private static int counter;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		textFieldMatch = "long";
		passwordMatch = new char[4];
		passwordMatch[0] = 'q';
		passwordMatch[1] = 's';
		passwordMatch[2] = 'c';
		passwordMatch[3] = 'v';
		type = 4;
		counter = 0;
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginUI window = new LoginUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		lblLogin = new JLabel("LOGIN");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 15));

		JLabel lblLoginName = new JLabel("Login Name:");

		textField = new JTextField();
		textField.setColumns(7);

		lblPassword = new JLabel("Password:");

		passwordField = new JPasswordField();
		btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//            				try {
				//        					tm.open();
				//        				} catch (UnknownHostException e1) {
				//        					// TODO Auto-generated catch block
				//        					e1.printStackTrace();
				//        				} catch (IOException e1) {
				//        					// TODO Auto-generated catch block
				//        					e1.printStackTrace();
				//        				}
				//        				try {
				//        					tm.send("login" + "," + textField.getText() + "," + passwordToString());
				//        				} catch (IOException e1) {
				//        					// TODO Auto-generated catch block
				//        					e1.printStackTrace();
				//        				}
				if(counter > 2){
					System.out.println("Unable to log in");
					return;
				}
				if(textField.getText().equalsIgnoreCase(textFieldMatch)){
					int x = 0;
					while(x < passwordMatch.length){
						if(passwordField.getPassword()[x] == passwordMatch[x]){
							x++;
						}
						else{
							counter++;
							x = passwordMatch.length + 1;
						}
					}
					if(x == passwordMatch.length){
						switch(type){
						case 1: PatientUI pUI = new PatientUI(); pUI.setVisible(true); frame.setVisible(false); frame.dispose(); break;
						case 2: NurseUI nUI = new NurseUI(); nUI.setVisible(true); frame.setVisible(false); frame.dispose(); break;
						case 3: DoctorUI dUI = new DoctorUI(); dUI.setVisible(true); frame.setVisible(false); frame.dispose(); break;
						case 4: AdminUI aUI = new AdminUI(); aUI.setVisible(true); frame.setVisible(false); frame.dispose(); break;
						}
					}
				}
				else{
					counter++;
				}
			}
		});
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
										.addGap(60)
										.addComponent(lblLoginName)
										.addGap(5)
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
										.addGroup(groupLayout.createSequentialGroup()
												.addGap(69)
												.addComponent(lblPassword)
												.addGap(5)
												.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
												.addGroup(groupLayout.createSequentialGroup()
														.addGap(342)
														.addComponent(btnLogin))
														.addGroup(groupLayout.createSequentialGroup()
																.addGap(187)
																.addComponent(lblLogin)))
																.addContainerGap(35, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addGap(24)
						.addComponent(lblLogin)
						.addGap(46)
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
										.addGap(3)
										.addComponent(lblLoginName))
										.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGap(10)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addGroup(groupLayout.createSequentialGroup()
														.addGap(3)
														.addComponent(lblPassword))
														.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
														.addGap(65)
														.addComponent(btnLogin))
		);
		frame.getContentPane().setLayout(groupLayout);
	}

	public void setVisible(boolean b) {
		frame.setVisible(b);            
	}

	public void setUser(String name){
		textField.setText(name);
	}
}
